//
//  GameControl.h
//  firstllk
//
//  Created by Huang Michael on 10-01-05.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "LineSprite.h"
#import "CDAudioManager.h"
#import "SimpleAudioEngine.h"

#define MAX_SPRITE_COL 7
#define MAX_SPRITE_ROW 8
#define MAX_SPRITES (MAX_SPRITE_COL * MAX_SPRITE_ROW)
#define START_TYPE_OF_SPRITE 14
#define MAXTYPE_OF_SPRITE 20
#define MAP_LEFT 20
#define MAP_UP	439
#define SPRITE_MAP_LEFT 20
#define SPRITE_MAP_UP 439
#define SPRITE_HEIGHT	40
#define SPRITE_WIDTH	40

#define GAMETIME	90
#define PAIR_BONUS	3
#define LIFE_COUNT 3
#define HINT_COUNT 3

//TAG DEFINE

//静态
#define TAG_BACKGROUND	901
#define TAG_LBSCORE		903
#define TAG_SCORE		904
#define TAG_LBLEVEL		905
#define TAG_TIMERPRORESS	908
#define TAG_PAUSE		910
#define TAG_REFRESH		911
#define TAG_HINT		912
#define	TAG_REFRESH_CNT	913
#define TAG_HINT_CNT	914
#define TAG_SOUND		915

//动态
#define TAG_SPMGR		100

#define TAG_LBFINAL			200
#define TAG_LBFINALSCORE	201

#define TAG_MNBACK		210
#define TAG_MNCONTINUE	211

#define TAG_SPBRUSH		300



#define SCORE_PER_PAIR 10

#define EN_SOUND

@interface SpriteStatus : NSObject{
	
@public CCSprite * sp ;
	int stype;
	BOOL isLife;
	CGPoint pos;
}
@end


typedef struct __sp_list{
	struct __sp_list * spNext;
	struct __sp_list * spPrev;
	SpriteStatus * spNode;
}ST_SPLIST;



@interface GameControl : CCLayer {

//	NSMutableArray *spPathArray;
	
	CGPoint spPathArray[5];
	int wayPointCnt;
	CCSpriteSheet *mgr;
	
	CCProgressTimer *prog;
	int gameTime;
	int levelTime;
	SpriteStatus * mapArray[MAX_SPRITE_COL+2][MAX_SPRITE_ROW+2];

	int cntArray[MAXTYPE_OF_SPRITE];
	//纪录当前生成地图上每种同类精灵的链表，用于快速判断是否有可配对精灵
	ST_SPLIST pSpListHead[MAXTYPE_OF_SPRITE];
	
	BOOL isSelect;
	BOOL isLined;
	BOOL isGameOver;
	BOOL isGameSuccessed;
	BOOL isAutoCheck;
	int nLevel;
	
	int nSpCounts;
	int nLifeCounts;
	int nHints;
	int nScore;
	int nCrtTypeOfSp;
	int nScorePerPair;
	
	CCMenuItem * mniHint;
	CCMenuItem * mniRefresh;
	CCMenuItem * mniPause;
	SpriteStatus * spsFirst;
	SpriteStatus * spsSecond;
	
	CCRenderTexture* rTexure;
	CCSprite* spBrush;

	CCLayer * gSelf;
	
	UIAlertView *changePlayerAlert;
	UITextField *changePlayerTextField;

}

- (void) initSpriteMap;

- (void) secondTimer  : (ccTime) dt;

//连线处理，依赖路径点计数和数组，依赖rTexture层，spBrush画刷精灵
- (void) drawline : (CGPoint)From : (CGPoint)To;

//负责清除路径图层，释放rTexture，spBrush画刷精灵
- (void) erasePath;

- (void) drawPath;

- (void) OnSetSound:(id) sender;

- (void) continueGmae:(id)sender;

- (void) returnMain:(id)sender;

- (void) pauseGame:(id)sender;

- (void) refreshSpriteMap:(id)sender;

- (void) checkAndDisplayHint:(id)sender;

-(void) timeout ;
-(void) gameover; 

- (void) getPlayerName;
- (BOOL)textFieldShouldReturn:(UITextField *)textField ;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex ;
- (void)saveHighScore ;

- (ST_SPLIST *) getNextValidSpNode: (ST_SPLIST *)node;

//调用路径搜索算法
- (BOOL) checkLink :(CGPoint) firtSp : (CGPoint) secondSp;

//一级路径搜索算法，依赖：精灵地图数组，路径点数组，路径点计数
- (BOOL) checkOneLine :(CGPoint) firtSp : (CGPoint) secondSp;

//一级路径搜索算法，依赖：精灵地图数组，路径点数组，路径点计数
- (BOOL) checkTwoLine :(CGPoint) firtSp : (CGPoint) secondSp;

//一级路径搜索算法，依赖：精灵地图数组，路径点数组，路径点计数
- (BOOL) checkThreeLine :(CGPoint) firtSp : (CGPoint) secondSp;

@end








